<?php

namespace App\Models;

use App\Util\Tool;
use App\Util\Url;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\ModelTree;
use Encore\Admin\Traits\AdminBuilder;

/**
 * 科室分类
 */
class ClinicCategory extends Model
{
    use ModelTree, AdminBuilder;

    protected $table = 'c_clinic_categories';

    protected $fillable = ['pid', 'classify_name', 'sort'];

    protected $with = [
        'parent'
    ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('pid');  // 父ID
        $this->setOrderColumn('sort'); // 排序
        $this->setTitleColumn('classify_name'); // 标题
        $this->setParentColumn('pid');//删除使用的pid
    }

    /**
     * 该分类的子分类
     */
    public function child()
    {
        return $this->hasMany(get_class($this), 'pid', $this->getKeyName());
    }

    /**
     * 该分类的父分类
     */
    public function parent()
    {
        return $this->hasOne(get_class($this), $this->getKeyName(), 'pid');
    }

    /**获取分类名称
     * @param $id
     * @return mixed
     */
    public function getName($id)
    {
        $res = $this->select('classify_name')->find($id);
        if (!empty($res)) {
            return $res->classify_name;
        } else {
            return '';
        }
    }

    /***
     * @return array
     * 医护室分类和问诊分类
     */

    public function getWorksCategoryList($param)
    {
        $where = [];
        $limit = 250;
        if (isset($param['pid'])) {
            $where[] = ['pid', '=', $param['pid']];
        }
        if (isset($param['title'])) {
            $where[] = ['classify_name', 'like','%'. $param['title'].'%'];
        }
        if (!empty($param['limit'])) {
            $limit = $param['limit'];
        }
        $res = $this::orderBy("sort")->where($where)->skip(0)->take($limit)->get()->toArray();
        $host = new Url();
        foreach ($res as $k => &$v) {
            $v['classify_picture'] = $host::getHttpHost() . 'upload/' . $v['classify_picture'];
        }
        $tree = (new Tool())::buildTree($res);
        return $tree;
    }


    /**
     * @param $param
     * android课程分类
     */
    public function getWorksCategoryListAndroid($param)
    {
        $where = [];
        $limit = 50;
        if (isset($param['pid'])) {
            $where[] = ['pid', '=', $param['pid']];
        }
        if (!empty($param['limit'])) {
            $limit = $param['limit'];
        }
        $res = $this::where($where)->skip(0)->orderBy("sort")->take($limit)->get()->toArray();
        $i = 0;
        $list = [];
        $data = [];
        $end = count($res) - 1;
        foreach ($res as $k => &$v) {
            if ($v['classify_picture']) {
                $v['classify_picture'] = Url::getHttpHost() . 'upload/' . $v['classify_picture'];
            }
            $data[] = $v;
            if ($i == 1 || $end == $k) {
                $list[] = $data;
                $data = [];
                $i = 0;
                continue;
            }
            $i++;
        }
        return $list;
    }

    /**
     * @param $param
     * @return array
     * 标签分类
     */
    public function getTargetCategoryList($param)
    {
        $where = [];
        $limit = 15;
        if (isset($param['pid'])) {
            if ($param['pid'] == 0) {
                $pid = $this->where([['pid', '=', 0]])->pluck('id');
                $where[] = [function ($query) use ($pid) {
                    return $query->whereIn('pid', $pid);
                }];
            } else {
                $where[] = ['pid', '=', $param['pid']];

            }
        }
        if (isset($param['title'])) {
            $where[] = ['classify_name', 'like', '%' . $param['title'] . '%'];
        }
        if (!empty($param['limit'])) {
            $limit = $param['limit'];
        }
        $res = $this::orderBy("sort")->where($where)->skip(0)->take($limit)->get()->toArray();
        $host = new Url();
        foreach ($res as $k => &$v) {
            $v['classify_picture'] = $host::getHttpHost() . 'upload/' . $v['classify_picture'];
            $td = $this->where([['pid','=',$v['id']]])->first();
            if(empty($td)  && $param['pid'] ==0 ){
                unset($res[$k]);
            }
        }
        $tree = (new Tool())::buildTree($res);
        return $tree;
    }

    /**
     * @return array
     * 获取分类列表
     */
    public function getOptions()
    {
        $options = $this->select('id', 'classify_name as text')->get();
        $selectOption = [];
        foreach ($options as $option) {
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }
}
